﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Smartmanufact
{
    public partial class Tiaojiansousuo : Form
    {
        
        public Tiaojiansousuo()
        {
            InitializeComponent();
        }

        public static DataSet returndataset=null;
        private string find_cmd = "";
        private string make_find_cmd()
        {
            string return_string = "";
            if(checkBox_carftname.CheckState==CheckState.Checked)
            {
                // 货品名
                return_string = "where cargoname='" + textBox_carftname.Text + "'";
            }
            if(checkBox_carft_kind.CheckState==CheckState.Checked)
            {
                // 货物种类
                return_string = "where cargokindid='"+comboBox_cargo_type.Text+"' ";
            }
            if(checkBox_chengpin.CheckState==CheckState.Checked)
            {
                // 货物是否成品
                if(return_string.Length==0)
                {
                    return_string = "where cargostate='" + comboBox_chengpin.Text + "' ";
                }
                else 
                {
                    return_string = return_string + " AND cargostate='" + comboBox_chengpin.Text + "' ";
                }
            }
            if(checkBox_cargo_carft.CheckState==CheckState.Checked)
            {
                // 货架
                if(return_string.Length==0)
                {
                    return_string = "where shelf='" + comboBox_cargo_carft.Text + "' ";
                }
                else
                {
                    return_string = return_string + " AND shelf='" + comboBox_cargo_carft.Text + "' ";
                }
            }
            
            if(checkBox_custmor.CheckState==CheckState.Checked)
            {
                // 客户
                if(return_string.Length==0)
                {
                    return_string = "where customername='" + comboBox_custmor_name.Text + "' ";
                }
                else
                {
                    return_string = return_string + " AND customername='" + comboBox_custmor_name.Text + "' ";
                }
            }

            if(checkBox_user.CheckState==CheckState.Checked)
            {
                // 操作人
                if(return_string.Length==0)
                {
                    return_string = "where username='" + comboBox_user.Text + "' ";
                }
                else
                {
                    return_string = return_string + " AND username='" + comboBox_user.Text + "' ";
                }
            }
            
            if(checkBox_savetime.CheckState==CheckState.Checked)
            {
                // 输入时间
                if(return_string.Length==0)
                {
                    return_string = "where savetime>'" + dateTimePicker_start.Value.ToString("yyyy-MM-dd HH:mm:ss") + " AND where savetime<'" + dateTimePicker_end.Value.ToString("yyyt-MM-dd HH:mm:ss") ;
                }
                else
                {
                    return_string=return_string+" AND savetime>'" + dateTimePicker_start.Value.ToString("yyyy-MM-dd HH:mm:ss") + " AND where savetime<'" + dateTimePicker_end.Value.ToString("yyyt-MM-dd HH:mm:ss") ;
                }
            }

            return return_string;
        }

        private void checkBox_carftname_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox_carftname.CheckState==CheckState.Checked)
            {
                // 控件在选中状态
                checkBox_carft_kind.CheckState = CheckState.Unchecked;
                checkBox_chengpin.CheckState = CheckState.Unchecked;
                checkBox_cargo_carft.CheckState = CheckState.Unchecked;
                checkBox_custmor.CheckState = CheckState.Unchecked;
                checkBox_user.CheckState = CheckState.Unchecked;
                checkBox_savetime.CheckState = CheckState.Unchecked;
            }
        }

        private void Tiaojiansousuo_Load(object sender, EventArgs e)
        {
            // 把项目加入到各个框中
            // 货物种类
            DataSet ds = null;
            int count = 0;
            try
            {
                ds = MainView.mysql.sql_search_database("select * from cargoinfotable");
                count = ds.Tables[0].Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    comboBox_cargo_type.Items.Add(ds.Tables[0].Rows[i][0].ToString());
                    // .Items.Add(ds.Tables[0].Rows[i][0].ToString());
                }
            }
            catch { }

            try
            {
                // 成品与非成品
                comboBox_chengpin.Items.Add("成品");
                comboBox_chengpin.Items.Add("未成品");
            }
            catch { }

            // 货架
            try
            {
                ds = MainView.mysql.sql_search_database("select * from enterpotname");
                count = ds.Tables[0].Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    comboBox_cargo_carft.Items.Add(ds.Tables[0].Rows[i][0].ToString());
                }
            }
            catch { }

            // 客户
            try
            {
                ds = MainView.mysql.sql_search_database("Select * from customertable");
                count = ds.Tables[0].Rows.Count;
                for (int i = 0; i < count; i++)
                {
                    comboBox_custmor_name.Items.Add(ds.Tables[0].Rows[i][0].ToString());
                }
            }
            catch { }
        }

        private void comboBox_user_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void checkBox_carft_kind_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox_carft_kind.CheckState==CheckState.Checked)
            {
                checkBox_carftname.CheckState = CheckState.Unchecked;
            }
        }

        private void checkBox_chengpin_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox_chengpin.CheckState==CheckState.Checked)
            {
                checkBox_carftname.CheckState = CheckState.Unchecked;
            }
        }

        private void checkBox_cargo_carft_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox_cargo_carft.CheckState==CheckState.Checked)
            {
                checkBox_carftname.CheckState = CheckState.Unchecked;
            }
        }

        private void checkBox_custmor_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox_custmor.CheckState==CheckState.Checked)
            {
                checkBox_carftname.CheckState = CheckState.Unchecked;
            }
        }

        private void checkBox_user_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox_user.CheckState==CheckState.Checked)
            {
                checkBox_carftname.CheckState = CheckState.Unchecked;
            }
        }

        private void checkBox_savetime_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox_user.CheckState==CheckState.Checked)
            {
                checkBox_user.CheckState = CheckState.Unchecked;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string cmd = "Select * from cargotable " + make_find_cmd();
                returndataset = MainView.mysql.sql_search_database(cmd);
                this.DialogResult = DialogResult.OK;

            }
            catch { }
        }
    }
}
